package org.example.reemploymentsys.controller;

import org.example.reemploymentsys.entity.Organization;
import org.example.reemploymentsys.entity.PublishList;
import org.example.reemploymentsys.entity.TrainingPlan;
import org.example.reemploymentsys.repository.OrganizationRe;
import org.example.reemploymentsys.repository.TrainingPlanRe;
import org.example.reemploymentsys.repository.UserTbRe;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class TrainingPlanController {
    @Autowired
    private UserTbRe userTbRe;
    @Autowired
    private TrainingPlanRe trainingPlanRe;
    @Autowired
    OrganizationRe organizationRe;
    @PostMapping("/getPlanList")
    public List<TrainingPlan> getPlanList(@RequestBody String username){
        System.out.println(username);
        String name= username.replace("\"","").replace("\"","");
        System.out.println(name);
        String OrganizationId= userTbRe.findOrgizationIdByUsername(name);
        System.out.println(OrganizationId);
        return trainingPlanRe.findTrainingPlanByOrganizationId(OrganizationId);
    }
    @PostMapping("/addPlan/{username}")
    public List<TrainingPlan> addPlan(@PathVariable String username, @RequestBody TrainingPlan trainingPlan){
        System.out.println(username);
        String name= username.replace("\"","").replace("\"","");
        System.out.println(name);
        String OrganizationId= userTbRe.findOrgizationIdByUsername(name);
        System.out.println(OrganizationId);
        Organization organization = organizationRe.findOrganizationById(OrganizationId);
        trainingPlan.setOrganization(organization);
        trainingPlanRe.save(trainingPlan);
        return trainingPlanRe.findTrainingPlanByOrganizationId(OrganizationId);
    }
    @PostMapping("/planDelete/{id}")
    public String PlanDelete(@PathVariable Long id){
        try {
            trainingPlanRe.deleteById(id);
            return "success";
        }catch (RuntimeException e){
            return "error";
        }
    }
    @PostMapping("/updatePlan")
    public String updatePlan(@RequestBody TrainingPlan trainingPlan){
        try {
            trainingPlanRe.save(trainingPlan);
            return "success";
        }catch (RuntimeException e){
            return "error";
        }
    }
}
